using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.UserCode;

namespace CKS.Sandbox.DailyDilbert.Proxy.Features.DailyDilbertProxyFeature
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("bdcd47f6-c84c-404c-a8de-da97d8f852cc")]
    public class DailyDilbertProxyFeatureEventReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Registers the Daily Dilbert Proxy Operation on feature activation
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPUserCodeService localSvc = SPUserCodeService.Local;

            SPProxyOperationType proxyOperationType =
                new SPProxyOperationType(
                "CKS.Sandbox.DailyDilbert.Proxy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7236f2dfb8dd25cd",
                "CKS.Sandbox.DailyDilbert.Proxy.DailyDilbertProxyOperation");

            if (!localSvc.ProxyOperationTypes.Contains(proxyOperationType))
            {
                localSvc.ProxyOperationTypes.Add(proxyOperationType);
                localSvc.Update();
            }
        }

        /// <summary>
        /// De-Registers the Daily Dilbert Proxy operation on feature de-activation
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPUserCodeService localSvc = SPUserCodeService.Local;

            SPProxyOperationType proxyOperationType =
                new SPProxyOperationType(
                "CKS.Sandbox.DailyDilbert.Proxy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7236f2dfb8dd25cd",
                "CKS.Sandbox.DailyDilbert.Proxy.DailyDilbertProxyOperation");

            if (localSvc.ProxyOperationTypes.Contains(proxyOperationType))
            {
                localSvc.ProxyOperationTypes.Remove(proxyOperationType);
                localSvc.Update();
            }
        }
    }
}
